package com.example.ccj.b.c.form;

import android.content.Context;
import androidx.annotation.NonNull;
import com.example.ccj.R;
import com.example.ccj.b.c.dialog.DialogBase;
import com.example.ccj.u.LogUtils;
import com.example.ccj.u.ToastUtils;

/**
 * 利用泛型创建表单
 * @author 驰珺
 * whechat   : chijun9
 * create at : 2024/6/25 13:32
 */
public class CreateFormDialog <T> extends DialogBase {

    CreateFormListener<T> createStudyListener;
    OptionFormatter optionFormatter;
    T object;
    Class<T> tClass;

    public CreateFormDialog(@NonNull Context context) {
        super(context);
    }

    public CreateFormDialog(@NonNull Context context,T object, Class<T> tClass) {
        super(context);
        this.object = object;
        this.tClass = tClass;
    }

    public CreateFormDialog(@NonNull Context context,T object, Class<T> tClass, CreateFormListener<T> createStudyListener) {
        super(context);
        this.createStudyListener = createStudyListener;
        this.object = object;
        this.tClass = tClass;
    }

    public CreateFormDialog(@NonNull Context context, T object, Class<T> tClass, CreateFormListener<T> createStudyListener,OptionFormatter optionFormatter) {
        super(context);
        this.createStudyListener = createStudyListener;
        this.optionFormatter = optionFormatter;
        this.object = object;
        this.tClass = tClass;
    }
    CreateFormView.MergeJavaBean<T> studyConfigMergeJavaBean = null;
    @Override
    public void onInit() {
        isCanClose(true);
        setLayout(R.layout.dialog_create_form);
        setAlpha(1f);//透明度
        isCanClose(true);//点击允许关闭
        isShowBackground(false);//不显示背景
        setShowHeightRatio(true);//按比例显示
        setHeightRatio(1);//高度显示比例
        showInBottom();//底部弹出
        setFull(true);

        CreateFormView form = findViewById(R.id.dialog_create_form_view);
        form.setLoadStateListener(new CreateFormView.LoadStateListener() {
            @Override
            public void onError(String error) {
                ToastUtils.showShort(error);
                LogUtils.e(error);
            }
            @Override
            public void onSuccess() {
                setButtonSureText("确定").setOnClickListener(v -> {
                    T t = studyConfigMergeJavaBean.getT();
                    if (t != null) {
                        if (createStudyListener != null) createStudyListener.onResult(t,CreateFormDialog.this);
                        dismiss();
                    } else {
                        if (createStudyListener != null) createStudyListener.onError(object,"创建失败！");
                        ToastUtils.showShort("创建失败！请检查是否填写正确");
                    }
                });
                setButtonCancelText("取消").setOnClickListener(v -> dismiss());
            }
        });
        studyConfigMergeJavaBean = form.setObject(object, tClass,optionFormatter);
    }


    public void setObject(T object,Class<T> tClass) {
        this.object = object;
        this.tClass = tClass;
    }


    public T getObject() {
        return object;
    }

    public void setObject(T object) {
        this.object = object;
    }

    public Class<T> gettClass() {
        return tClass;
    }

    public void settClass(Class<T> tClass) {
        this.tClass = tClass;
    }


    public CreateFormListener<T> getCreateStudyListener() {
        return createStudyListener;
    }

    public void setCreateStudyListener(CreateFormListener<T> createStudyListener) {
        this.createStudyListener = createStudyListener;
    }

    public OptionFormatter getOptionFormatter() {
        return optionFormatter;
    }

    public void setOptionFormatter(OptionFormatter optionFormatter) {
        this.optionFormatter = optionFormatter;
    }


    public interface CreateFormListener <T>{
        void onResult(T t ,CreateFormDialog<T> createFormDialog);
        void onError(T t,String error);
    }
}
